from xml.dom.minidom import Document,parse
import time
from settings import ASK_MSG_PATH,MSG_SEND_PATH,MSG_RECV_PATH

def pack_are_you_there_request_msg_xml(msgName,bodyDict,retCode):
    """
    构建Are You There Request 的内容
    :param msgName:
    :param bodyDict:
    :param retCode:
    :return:
    """
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    transactionIdUsed = get_transaction_id()
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(ASK_MSG_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_recipe_parameter_reqeust_reply_data(msgName,bodyDict,data,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
        recipeList = [
            ("R01",data.get("id","工单不存在")),
            ("R02",round(float(data.get("height",0) * 25.4)),1),
            ("R03",round(float(data.get("width",0) * 25.4)),2),
            ("R04",data.get("cropperThick",0)),
            ("R05",data.get("topEdge",0))
        ]

        traceDataListNode = doc.createElement("recipe_parameter_list")
        for data in recipeList:
            traceListNode = doc.createElement("recipe_parameter")
            dataItemNode = doc.createElement("item_name")
            dataValueNode = doc.createElement("item_value")
            dataItemNode.appendChild(doc.createTextNode(data[0]))
            dataValueNode.appendChild(doc.createTextNode(str(data[1])))
            traceListNode.appendChild(dataItemNode)
            traceListNode.appendChild(dataValueNode)
            traceDataListNode.appendChild(traceListNode)
        bodyNode.appendChild(traceDataListNode)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_process_data(msgName,bodyDict,process_data_list,retCode,transactionId):
  doc = Document()  # 在内存中创建一个空的文档
  root = doc.createElement("message")  # 创建根节点
  doc.appendChild(root)  # 将根节点添加到文件对象中

  # 然后将head的消息和body的消息以及ret的消息填入
  headNode = doc.createElement("header")  # 创建head节点
  msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
  msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
  if transactionId is None:
    transactionIdUsed = get_transaction_id()
  else:
    transactionIdUsed = transactionId
  idNode = doc.createElement("transactionid")
  idNode.appendChild(doc.createTextNode(transactionIdUsed))
  headNode.appendChild(msgNameNode)
  headNode.appendChild(idNode)

  bodyNode = doc.createElement("body")
  if len(bodyDict.items()) == 0:
    bodyNode.appendChild(doc.createTextNode(" "))
  else:
    for element, value in bodyDict.items():
      node = doc.createElement(element)
      node.appendChild(doc.createTextNode(value))
      bodyNode.appendChild(node)
    traceDataListNode = doc.createElement("proc_data_list")
    for data in process_data_list:
      traceListNode = doc.createElement("proc_data")
      dataItemNode = doc.createElement("data_item")
      dataValueNode = doc.createElement("data_value")
      dataItemNode.appendChild(doc.createTextNode(data[0]))
      dataValueNode.appendChild(doc.createTextNode(str(data[1])))
      traceListNode.appendChild(dataItemNode)
      traceListNode.appendChild(dataValueNode)
      traceDataListNode.appendChild(traceListNode)
    bodyNode.appendChild(traceDataListNode)
  retNode = doc.createElement("return")
  for element, value in retCode.items():
    node = doc.createElement(element)
    if value != "":
      node.appendChild(doc.createTextNode(value))
    retNode.appendChild(node)

  # 然后将head_node,body_node,ret_node加到根节点root节点上面去
  root.appendChild(headNode)
  root.appendChild(bodyNode)
  root.appendChild(retNode)

  fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
  doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
  fp1.close()

def pack_alarm_report_data(msgName,bodyDict,alarmCode,retCode,transactionId):
  doc = Document()  # 在内存中创建一个空的文档
  root = doc.createElement("message")  # 创建根节点
  doc.appendChild(root)  # 将根节点添加到文件对象中

  # 然后将head的消息和body的消息以及ret的消息填入
  headNode = doc.createElement("header")  # 创建head节点
  msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
  msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
  if transactionId is None:
    transactionIdUsed = get_transaction_id()
  else:
    transactionIdUsed = transactionId
  idNode = doc.createElement("transactionid")
  idNode.appendChild(doc.createTextNode(transactionIdUsed))
  headNode.appendChild(msgNameNode)
  headNode.appendChild(idNode)


  bodyNode = doc.createElement("body")
  if len(bodyDict.items()) == 0:
    bodyNode.appendChild(doc.createTextNode(" "))
  else:
    for element, value in bodyDict.items():
      node = doc.createElement(element)
      node.appendChild(doc.createTextNode(value))
      bodyNode.appendChild(node)
    alarmData = [alarmCode]
    traceDataListNode = doc.createElement("alarm_list")
    for data in alarmData:
      traceListNode = doc.createElement("alarm")
      dataItemNode = doc.createElement("alarm_code")
      dataItemNode.appendChild(doc.createTextNode(data))
      traceListNode.appendChild(dataItemNode)
      traceDataListNode.appendChild(traceListNode)
    bodyNode.appendChild(traceDataListNode)
  retNode = doc.createElement("return")
  for element, value in retCode.items():
    node = doc.createElement(element)
    if value != "":
      node.appendChild(doc.createTextNode(value))
    retNode.appendChild(node)

  # 然后将head_node,body_node,ret_node加到根节点root节点上面去
  root.appendChild(headNode)
  root.appendChild(bodyNode)
  root.appendChild(retNode)

  fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
  doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
  fp1.close()

def pack_trace_data_replay_msg_xml(msgName,bodyDict,traceDataList, retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)

        traceDataListNode = doc.createElement("trace_data_list")
        for data in traceDataList:
            traceListNode = doc.createElement("trace_data")
            dataItemNode = doc.createElement("data_item")
            dataValueNode = doc.createElement("data_value")
            dataItemNode.appendChild(doc.createTextNode(data[0]))
            dataValueNode.appendChild(doc.createTextNode(str(data[1])))
            traceListNode.appendChild(dataItemNode)
            traceListNode.appendChild(dataValueNode)
            traceDataListNode.appendChild(traceListNode)
        bodyNode.appendChild(traceDataListNode)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()


def pack_init_data_replay_msg_xml(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_date_time_sysnc_replay_data(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_job_data_download_reply_data(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_remote_control_command_reply_data(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_equipment_job_data_process_report_data(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(str(value)))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_eqp_current_status_data(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_eqp_control_mode_data(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()

def pack_control_mode_command_reply_data(msgName,bodyDict,retCode,transactionId):
    doc = Document()  # 在内存中创建一个空的文档
    root = doc.createElement("message")  # 创建根节点
    doc.appendChild(root)  # 将根节点添加到文件对象中

    # 然后将head的消息和body的消息以及ret的消息填入
    headNode = doc.createElement("header")  # 创建head节点
    msgNameNode = doc.createElement("messagename")  # 创建消息名称节点
    msgNameNode.appendChild(doc.createTextNode(msgName))  # 消息名称的文字节点
    if transactionId is None:
        transactionIdUsed = get_transaction_id()
    else:
        transactionIdUsed = transactionId
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionIdUsed))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    bodyNode = doc.createElement("body")
    if len(bodyDict.items()) == 0:
        bodyNode.appendChild(doc.createTextNode(" "))
    else:
        for element, value in bodyDict.items():
            node = doc.createElement(element)
            node.appendChild(doc.createTextNode(value))
            bodyNode.appendChild(node)
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    # 然后将head_node,body_node,ret_node加到根节点root节点上面去
    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp1 = open(MSG_SEND_PATH, 'w', encoding='UTF-8')
    doc.writexml(fp1, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp1.close()


def parse_recv_msg(msgRecv):
    # 1.先将消息写入到文件当中
    write_recv_message(msgRecv)
    dom = parse(MSG_RECV_PATH)
    rootNode = dom.documentElement
    msgNameNode = rootNode.getElementsByTagName("messagename")[0]
    transactionNode = rootNode.getElementsByTagName("transactionid")[0]
    bodyDict = {}
    bodyNode = rootNode.getElementsByTagName("body")[0]
    msgName = msgNameNode.childNodes[0].data
    if msgName == "JobDataDownload":
        # write_recipe_params(msgRecv)
        print("接收到JobDataDownload 消息:")
        bodyDict = {}
        for node in bodyNode.childNodes:
            # if node.nodeName == "recipe_name":
            #     bodyDict[node.nodeName] = node.childNodes[0].data
            #     continue

            if node.nodeName == "total_panel_count":
                bodyDict[node.nodeName] = node.childNodes[0].data
                continue

            if node.nodeName == "job_id":
                bodyDict[node.nodeName] = node.childNodes[0].data
                continue

            if node.nodeName == "recipe_parameter_list":
                for d in node.childNodes:
                    if d.nodeName == "recipe_parameter":
                        for d1 in d.childNodes:
                            if d1.nodeName == "item_name":
                                dictKey = d1.childNodes[0].data
                            if d1.nodeName == "item_value":
                                dictVal = d1.childNodes[0].data
                        else:
                            bodyDict[dictKey] = dictVal
    else:
        for node in bodyNode.childNodes:
            if node.nodeName != "#text" and node.nodeName != "user_id":
                bodyDict[node.nodeName] = node.childNodes[0].data if len(node.childNodes) > 0 else ""

    retNode = rootNode.getElementsByTagName("returncode")[0]
    retCode = retNode.childNodes[0].data if len(retNode.childNodes) > 0 else ""

    if "Reply" not in msgName:
        transactionId = transactionNode.childNodes[0].data
    else:
        transactionId = get_transaction_id()

    print(msgName,bodyDict,retCode,transactionId)

    return msgName, bodyDict, retCode, transactionId

def write_recv_message(msgRecv):
    """
    将接收到的消息写入到文件message_recv.xml中，以便后续的解析
    :param msgRecv: 接收到的消息，包括消息头和消息结尾char(2)和char(3)
    :return:
    """
    # 如果是16进制的,就先解码
    decodedData = msgRecv[1:-1]
    strData = decodedData.decode("utf-16")
    with open(MSG_RECV_PATH, "w", encoding="utf-8") as f:
        f.write(strData)

def get_transaction_id():
    nowSecond = time.time()
    lastThree = str(nowSecond).split(".")[1][:3]
    now = time.strftime("%Y%m%d%H%M%S", time.localtime(nowSecond))
    transactionId = now + lastThree
    return transactionId




if __name__ == '__main__':
    data = b'\x02\xff\xfe<\x00?\x00x\x00m\x00l\x00 \x00v\x00e\x00r\x00s\x00i\x00o\x00n\x00=\x00"\x001\x00.\x000\x00"\x00 \x00e\x00n\x00c\x00o\x00d\x00i\x00n\x00g\x00=\x00"\x00U\x00T\x00F\x00-\x008\x00"\x00?\x00>\x00\n\x00<\x00m\x00e\x00s\x00s\x00a\x00g\x00e\x00>\x00\n\x00\t\x00<\x00h\x00e\x00a\x00d\x00e\x00r\x00>\x00\n\x00\t\x00\t\x00<\x00m\x00e\x00s\x00s\x00a\x00g\x00e\x00n\x00a\x00m\x00e\x00>\x00A\x00r\x00e\x00Y\x00o\x00u\x00T\x00h\x00e\x00r\x00e\x00R\x00e\x00q\x00u\x00e\x00s\x00t\x00<\x00/\x00m\x00e\x00s\x00s\x00a\x00g\x00e\x00n\x00a\x00m\x00e\x00>\x00\n\x00\t\x00\t\x00<\x00t\x00r\x00a\x00n\x00s\x00a\x00c\x00t\x00i\x00o\x00n\x00i\x00d\x00>\x002\x000\x002\x004\x000\x006\x002\x009\x001\x001\x002\x007\x001\x001\x005\x002\x004\x00<\x00/\x00t\x00r\x00a\x00n\x00s\x00a\x00c\x00t\x00i\x00o\x00n\x00i\x00d\x00>\x00\n\x00\t\x00<\x00/\x00h\x00e\x00a\x00d\x00e\x00r\x00>\x00\n\x00\t\x00<\x00b\x00o\x00d\x00y\x00>\x00\n\x00\t\x00\t\x00<\x00e\x00q\x00p\x00_\x00i\x00d\x00>\x00H\x00D\x000\x000\x001\x00<\x00/\x00e\x00q\x00p\x00_\x00i\x00d\x00>\x00\n\x00\t\x00\t\x00<\x00s\x00e\x00r\x00v\x00e\x00r\x00_\x00i\x00p\x00>\x001\x002\x007\x00.\x000\x00.\x000\x00.\x001\x00<\x00/\x00s\x00e\x00r\x00v\x00e\x00r\x00_\x00i\x00p\x00>\x00\n\x00\t\x00<\x00/\x00b\x00o\x00d\x00y\x00>\x00\n\x00\t\x00<\x00r\x00e\x00t\x00u\x00r\x00n\x00>\x00\n\x00\t\x00\t\x00<\x00r\x00e\x00t\x00u\x00r\x00n\x00c\x00o\x00d\x00e\x00>\x000\x00<\x00/\x00r\x00e\x00t\x00u\x00r\x00n\x00c\x00o\x00d\x00e\x00>\x00\n\x00\t\x00\t\x00<\x00r\x00e\x00t\x00u\x00r\x00n\x00m\x00e\x00s\x00s\x00a\x00g\x00e\x00/\x00>\x00\n\x00\t\x00<\x00/\x00r\x00e\x00t\x00u\x00r\x00n\x00>\x00\n\x00<\x00/\x00m\x00e\x00s\x00s\x00a\x00g\x00e\x00>\x00\n\x00\x03'
    write_recv_message(data)
